/*
 * @lc app=leetcode.cn id=24 lang=java
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    /**
    * Accepted
    55/55 cases passed (0 ms)
    Your runtime beats 100 % of java submissions
    Your memory usage beats 78.19 % of java submissions (39 MB)
    */
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0, head);
        ListNode pre = dummy, first = head;
        while (first != null && first.next != null) {
            pre.next = first.next;
            first.next = pre.next.next;
            pre.next.next = first;
            pre = first;
            first = pre.next;
        }
        return dummy.next;
    }
}
// @lc code=end
